package com.ustb.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ustb.springboot.common.utils.R;
import com.ustb.springboot.common.utils.TokenUtil;
import com.ustb.springboot.dao.AnswerRadioDao;
import com.ustb.springboot.entity.*;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ustb.springboot.dao.AnswerSlideDao;
import com.ustb.springboot.service.AnswerSlideService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("answerSlideService")
public class AnswerSlideServiceImpl extends ServiceImpl<AnswerSlideDao, AnswerSlideEntity> implements AnswerSlideService {
    @Resource
    private AnswerSlideDao answerSlideDao;

    @Override
    public R save(HttpServletRequest request, AnswerSlideEntity answerSlide) {
        //设置userId
        answerSlide.setUserId(TokenUtil.getId(request));

        //保存成功或失败
        return this.saveOrUpdate(answerSlide) ? R.ok(20000, "保存答案成功！") : R.error(2000, "保存答案失败！");
    }

    @Override
    public R delete(Map<String, Object> body) {
        //根据id查询删除
        QueryWrapper<AnswerSlideEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("id"));
        return answerSlideDao.delete(queryWrapper) == 1 ? R.ok(20000, "删除成功！") : R.error(2000, "删除失败！");
    }

    @Override
    public List<AnswerSlideEntity> queryAnsSlidewithQuestionId(Map<String, Object> body) {
        QueryWrapper<AnswerSlideEntity> answerSlideEntityQueryWrapper = new QueryWrapper<>();
        answerSlideEntityQueryWrapper.eq("question_id" , body.get("question_id"));
        List<AnswerSlideEntity> answerSlideEntityList = answerSlideDao.selectList(answerSlideEntityQueryWrapper);
        return answerSlideEntityList;
    }

    @Override
    public Map<String, Object> summerizeSlide(Map<String, Object> body) {
        List <AnswerSlideEntity> answerSlideEntityList = queryAnsSlidewithQuestionId(body);
        //List <QuestionScoreEntity> questionScoreEntityList =.queryQuestionScore(body);
        List<Object> TotalMessage = new ArrayList<>();
        for (AnswerSlideEntity answerSlideEntity : answerSlideEntityList){
            List<Object> message = new ArrayList<>();
            message.add(answerSlideEntity.getAnswer());
            TotalMessage.add(message);
        }
        Map<String,Object> summerizeMessage =new HashMap<>();
        summerizeMessage.put("totalMessage",TotalMessage);
        summerizeMessage.put("sum",answerSlideEntityList.size());
        return summerizeMessage;
    }

}